package com.itheima.service;

import com.itheima.admin.AdminPage;
import com.itheima.mapper.admin.OrderMapper;
import com.itheima.admin.orderpojo.Order;
import com.itheima.admin.orderpojo.vo.OrderQueryVo;
import com.itheima.admin.orderpojo.vo.OrderStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wuzhiyong
 */
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 分页
     *
     * @param orderQueryVo orderQueryVo
     * @return adminPage
     */
    public AdminPage<Order> getpageOrder(OrderQueryVo orderQueryVo) {
        Integer page = orderQueryVo.getPage();
        Integer pageSize = orderQueryVo.getPageSize();
        int startIndex = (page - 1) * pageSize;
        orderQueryVo.setPage(startIndex);
        int total = orderMapper.selectAllOrder();
        List<Order> orders = orderMapper.selectPageOrder(orderQueryVo);
        AdminPage<Order> adminPage = new AdminPage<>();
        adminPage.setSize(pageSize);
        adminPage.setCurrent(page);
        adminPage.setTotal(total);
        adminPage.setRecords(orders);
        return adminPage;
    }

    /**
     * 修改状态
     * @param orderStatusVo orderStatusVo
     * @return 条数>0
     */
    public boolean modifyStatusById(OrderStatusVo orderStatusVo) {
        return orderMapper.updateStatusById(orderStatusVo) > 0;
    }
}
